package com.xsw.web.controller.system;

import com.xsw.common.annotation.Anonymous;
import com.xsw.common.constant.Constants;
import com.xsw.common.core.domain.AjaxResult;
import com.xsw.common.core.domain.R;
import com.xsw.common.core.domain.entity.SysMenu;
import com.xsw.common.core.domain.entity.SysRole;
import com.xsw.common.core.domain.entity.SysUser;
import com.xsw.common.core.domain.model.LoginBody;
import com.xsw.common.core.domain.model.LoginUser;
import com.xsw.common.core.domain.model.MiniLoginBody;
import com.xsw.common.utils.SecurityUtils;
import com.xsw.framework.web.service.SysLoginService;
import com.xsw.framework.web.service.SysPermissionService;
import com.xsw.framework.web.service.TokenService;
import com.xsw.system.service.ISysMenuService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Api(tags = "登录验证")
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 微信登录
     *
     * @param loginDTO 登录信息
     * @return 结果
     */
    @PostMapping("/mini/login")
    @Anonymous//放行该请求
    public R miniLogin(@RequestBody MiniLoginBody loginDTO) {
        // 生成令牌
        String token = loginService.miniLogin(loginDTO);
        return R.ok(token);
    }

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid(), loginBody.getRoleId(), loginBody.getStoreId());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        // 角色集合
//        Set<String> roles = permissionService.getRolePermission(user);
        //当前登录用的角色【唯一】
        Set<String> roles = user.getRoles().stream().map(SysRole::getRoleKey).collect(Collectors.toSet());
        // 权限集合【当前登录用的角色的权限集合】
        Set<String> permissions = permissionService.getMenuPermission(user);
        if (!loginUser.getPermissions().equals(permissions)) {
            loginUser.setPermissions(permissions);
            tokenService.refreshToken(loginUser);
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);

        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public R getRouters() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserRoleId(user);
        return R.ok(menuService.buildMenus(menus));
    }
}
